package com.tiantianfresh.view.service.impl;

import com.tiantianfresh.admin.util.type;
import com.tiantianfresh.model.dao.CommodityAllocationMapper;
import com.tiantianfresh.model.dao.CommodityMapper;
import com.tiantianfresh.model.dao.OrderMapper;
import com.tiantianfresh.model.dao.OrderSpecificMapper;
import com.tiantianfresh.model.pojo.*;
import com.tiantianfresh.view.service.DetailPageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author FANGAO
 * @date 2021/11/30 - 上午 11:01
 */
@Service
public class DetailPageServiceImpl implements DetailPageService {
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private CommodityAllocationMapper commodityAllocationMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderSpecificMapper orderSpecificMapper;

    @Override
    public CommodityResult queryCommodity(Integer id) {
        return commodityMapper.queryCommodity(id);
    }

    @Override
    public List<CommodityAllocation> queryCommodityAllocationAll(Integer comid) {

        return commodityAllocationMapper.queryCommodityAllocationAll(comid);
    }

    @Override
    public Order addOrder(Map<String, Integer> map) {
        CommodityResult caid = commodityMapper.queryCommodity(map.get("caid"));
        Order order = new Order();
        order.setOrderid(type.getRandomandDatetime());
        order.setOrderdate(new Date());

        order.setUserid(map.get("userid"));
        order.setOrderState(1);
        order.setAddressId(0);
        OrderSpecific orderSpecific = new OrderSpecific();
        orderSpecific.setCommodityname(caid.getCommodityname());
        orderSpecific.setCommoditypath(caid.getCommoditypath());
        orderSpecific.setCompany(caid.getCompany());
        if(caid.getStatebackdrop()!=null&&caid.getStatebackdrop().equals("best")){
            double v = type.percentageToDouble(caid.getCommoditystate());
            order.setTotalPrice(caid.getPrice()*v*map.get("quantity"));
            orderSpecific.setPrice(caid.getPrice()*v);
        }else{
            order.setTotalPrice(caid.getPrice()*0.95*map.get("quantity"));
            orderSpecific.setPrice(caid.getPrice()*0.95);
        }
        orderSpecific.setQuantity(map.get("quantity"));
        orderSpecific.setOrderid(order.getOrderid());
        orderSpecific.setUserid(map.get("userid"));
        if (orderSpecificMapper.addOrderSpecific(orderSpecific) > 0 && orderMapper.addOrder(order) > 0) return order;
        return null;
    }
}
